package cn.wolfcode.luowowo.wesite.controller;

import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.vo.SearchResultVo;
import cn.wolfcode.luowowo.wesite.annotation.RequireLogin;
import cn.wolfcode.luowowo.wesite.annotation.UserParam;
import cn.wolfcode.luowowo.wesite.until.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by LENOVO on 2019/8/8.
 */
@Controller

public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationTemplateSearchService destinationTemplateSearchService;

    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;

    @Reference
    private ITravelTemplateSearchService travelTemplateSearchService;

    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;

    @Reference
    private ISearchService searchService;

    //必须要登录才能放行
    //@RequireLogin
    @RequestMapping("")
    public String index(Model model, HttpServletRequest request){
        String token = CookieUtil.getToken(request);
        //拿到的token不爲空
        if(token !=null){
            UserInfo userInfo = userInfoRedisService.getUserByToken(token);
            request.getSession().setAttribute("userInfo",userInfo);
        }


        //首页游记的推荐
        model.addAttribute("tcs",travelCommendService.getTop5());
        //攻略推荐的  scs
        model.addAttribute("sc",strategyCommendService.getTop5().get(0));



        return "index/index";
    }
    @RequestMapping("/index/travelPage")
    public String travelPage(Model model,@ModelAttribute("qo") TravelQuery qo){

        model.addAttribute("pageInfo",travelService.query(qo));
        return "index/travelPageTpl";
    }


    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo")SearchQueryObject qo){
        switch (qo.getType()){
            case  SearchQueryObject.CONDITION_TYPE_DEST:
                return  destSearch(model,qo);
            case  SearchQueryObject.CONDITION_TYPE_STRATEGY:
                return  strateguSearch(model,qo);
            case  SearchQueryObject.CONDITION_TYPE_TRAVEL:
                return  travelSearch(model,qo);
            case  SearchQueryObject.CONDITION_TYPE_USER:
                return  userSearch(model,qo);
            default:return  allSearch(model,qo);//所有的
        }
    }

    //检索目的地
    private String destSearch(Model model, SearchQueryObject qo) {
        SearchResultVo data =new SearchResultVo();
        //查询目的地对象
        DestinationTemplate dest=destinationTemplateSearchService.findByName(qo.getKeyword());

        if(dest !=null){
            //查询该目的地下的所有攻略 ，前五个
            long total=0;
            List<StrategyTemplate> sts=strategyTemplateSearchService.findByDestName(dest.getName());
            total+=sts.size();
            if(sts.size()>5){
                sts=sts.subList(0,5);
            }

            //查询该目的地下所有的游记 前五个
            List<TravelTemplate> ts=travelTemplateSearchService.findByDestName(dest.getName());
            total+=ts.size();
            if(ts.size()>5){
                ts=ts.subList(0,5);
            }

            //查询该目的地下的所有用户前五个
            List<UserInfoTemplate> us=userInfoTemplateSearchService.findByDestName(dest.getName());
            total+=us.size();
            if(us.size()>5){
                us=us.subList(0,5);
            }
            data.setStrategys(sts);
            data.setTravels(ts);
            data.setUsers(us);
            data.setTotal(total);
            model.addAttribute("data",data);
        }

        model.addAttribute("dest",dest);
        return "index/searchDest";
    }
    //攻略
    private String strateguSearch(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> page = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle","summary");
        model.addAttribute("page",page);
        return "index/searchStrategy";
    }

    //游记
    private String travelSearch(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> page = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page",page);
        return "index/searchTravel";
    }

    //人
    private String userSearch(Model model, SearchQueryObject qo) {
        //参数1 索引   参数二是类型 ,参数三,结果对象的泛型, 参数四  qo对象  ,参数五是查询字段
        Page<UserInfoTemplate> page = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page",page);
        return "index/searchUser";
    }

    //所有的
    private String allSearch(Model model, SearchQueryObject qo) {
        SearchResultVo data = new SearchResultVo();
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo, "title", "subTitle","summary");
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo, "title", "summary");
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo, "nickname", "destName");
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo, "name", "info");


        data.setUsers(users.getContent());
        data.setTravels(travels.getContent());
        data.setDests( dests.getContent());
        data.setStrategys(strategys.getContent());

        data.setTotal(strategys.getTotalElements()+travels.getTotalElements()+users.getTotalElements()+dests.getTotalElements());
        model.addAttribute("data",data);
        return "index/searchAll";
    }




























        @RequestMapping("userInfo1")
    @ResponseBody
    public Object userInfo1(HttpServletRequest request,UserInfo userInfo){
        return userInfo;
    }
    @RequestMapping("userInfo2")
    @ResponseBody
    public Object userInfo2(HttpServletRequest request,@UserParam UserInfo userInfo){
        return userInfo;
    }
}
